/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <cmsis_os2.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "iot_main.h"
#include "cmsis_os2.h"
#include "ohos_init.h"
#include "i2c_test.h"
#include "gpio_if.h"
#include "iot_nfc.h"

#include "iot_profile.h"
#include "iot_cloud.h"
#include "iot_netcfg.h"
#include "iot_sntp.h"
#include "iot_wifi.h"
#include "kv_store.h"
#include "network_config_service.h"
#include "ohos_init.h"
#include "iot_demo_def.h"
#include "iot_weather.h"
#include "iot_schedule.h"
#include "iot_uart.h"
#include "nativeapi_config.h"

#define SID_KEY "ssid"
#define PWD_KEY "password"
#define max_len 128

// DEFAULT PARAM, AND MUST BE MODIFIED TO THE REAL PARAMS
#define CONFIG_CLOUD_DEFAULT_SERVERIP "121.36.42.100" // replace it with iotplatform server ip
#define CONFIG_CLOUD_DEFAULT_SERVERPORT "1883"        // replace it with iotplatform server  port
#define CONFIG_SENSOR_SAMPLE_CYCLE 30000                  // unit:minisecond

#define CN_LAMP_EVENT_ALL   (CN_SWITCH_EVENT_SETSTATUS)
#define CN_SWITCH_EVENT_SETSTATUS  (1<<0)
#define CONFIG_LOCAL_TIMEZONE            8

#define CONFIG_MAX_SEND_DATA_LEN         256

static const uint16_t GPIO02 = 2;

typedef struct {
     int startHour;
     int startMinute;
     int DurationMinutes;
     int status;
}ScheduleDisplay;

typedef struct {
     int year;
     int month;
     int day;
     int week;
}Date;

typedef struct {
    int curDay;
    int curSecondsInDay;
    osTimerId_t timeID;
    int executeTime;
    int timerFlag;
    osEventFlagsId_t switchEvent;
    bool switchSts;
    int humidityValue;
    int HumidityMax;
    bool wifiSts;
    ScheduleDisplay scheduleDisplay[10];
    unsigned char totalScheduleNumbers;
    int lastTotalScheduleNumbers;
    Date date;
    bool getNtpTime;
    bool getWeather;
}ApplicationController;

char g_sendMessage[CONFIG_MAX_SEND_DATA_LEN];
static ApplicationController g_appController;

#define TIMER_TICK_NUM 900

void IotMain_SetWeatherStatus(bool value)
{
    g_appController.getWeather = value;
}

static bool IotMain_GetWeatherStatus(void)
{
    return g_appController.getWeather;
}


void IotMain_Init(void) {
    g_appController.HumidityMax = 40;
    g_appController.humidityValue = 0;
    g_appController.switchSts = false;
    g_appController.wifiSts = false;
    g_appController.totalScheduleNumbers = 0;
    g_appController.lastTotalScheduleNumbers = 0;
    g_appController.getNtpTime = false; 
}

void IotMain_AddScheduleDisplay(CommandParamSetShedule setSheduleParam)
{
    HILOG_INFO(HILOG_MODULE_APP, "HILOG_INFO %s:g_appController.totalScheduleNumbers=%d--------------------------\r\n", __func__,g_appController.totalScheduleNumbers);
    if (g_appController.totalScheduleNumbers < 10) {
       g_appController.scheduleDisplay[g_appController.totalScheduleNumbers].startHour = setSheduleParam.startHour;
       g_appController.scheduleDisplay[g_appController.totalScheduleNumbers].startMinute = setSheduleParam.startMinute;
       g_appController.scheduleDisplay[g_appController.totalScheduleNumbers].DurationMinutes = setSheduleParam.duration;
       g_appController.scheduleDisplay[g_appController.totalScheduleNumbers].status = setSheduleParam.switchStatus.status;
       g_appController.totalScheduleNumbers++;

        HILOG_INFO(HILOG_MODULE_APP, "setSheduleParam.startHour=%d--------------------------\r\n",setSheduleParam.startHour);
        HILOG_INFO(HILOG_MODULE_APP, "setSheduleParam.startMinute=%d--------------------------\r\n",setSheduleParam.startMinute);
        HILOG_INFO(HILOG_MODULE_APP, "setSheduleParam.duration=%d--------------------------\r\n",setSheduleParam.duration);
        HILOG_INFO(HILOG_MODULE_APP, "setSheduleParam.status=%d--------------------------\r\n",setSheduleParam.switchStatus.status);
        HILOG_INFO(HILOG_MODULE_APP, "scheduleDisplay[g_appController.totalScheduleNumbers].startHour=%d--------------------------\r\n",\
        g_appController.scheduleDisplay[g_appController.totalScheduleNumbers].startHour);
    }else {
        g_appController.totalScheduleNumbers = 0;
    }
}

static void IotMain_CtrlSetGpioLow(void)
{
    GpioSetDir(GPIO02, GPIO_DIR_OUT);
    GpioWrite(GPIO02, GPIO_VAL_LOW);
}


static void IotMain_CtrlSetGpioHight(void)
{
    GpioSetDir(GPIO02, GPIO_DIR_OUT);
    GpioWrite(GPIO02, GPIO_VAL_HIGH);
}

void IotMain_SetSwitchSts( bool value) {
    g_appController.switchSts = value;
}

static void IotMain_CtrlSwitch(bool value) {
    if (value == true) {
        IotMain_CtrlSetGpioHight();
    } else {
        IotMain_CtrlSetGpioLow();
    }
    HILOG_INFO(HILOG_MODULE_APP, "HILOG_INFO %s:SetMotorSts=%d\r\n", __func__,g_appController.switchSts);
}


bool IotMain_GetMotorSts(void) {
    return g_appController.switchSts;
}

void IotMain_JsSet(bool value)
{
    IotMain_SetSwitchSts(value);
    osEventFlagsSet(g_appController.switchEvent, CN_SWITCH_EVENT_SETSTATUS);
}

void IotMain_VoiceSet(bool value )
{
    IotMain_SetSwitchSts(value);
    osEventFlagsSet(g_appController.switchEvent, CN_SWITCH_EVENT_SETSTATUS);   
}

void IotMain_JsSetHumidityThreshold(const char *value)
{
    if(value == NULL ) {
        return ;
    }

    int data = atoi(value);
    RaiseLog(LOG_LEVEL_INFO, "value = %s,data:%d.\n",value,data);
    if( data >= 40 && data <= 80) {
        g_appController.HumidityMax = data;
        osEventFlagsSet(g_appController.switchEvent, CN_SWITCH_EVENT_SETSTATUS);
    }
}



static int IotMain_JsGetClang(const char* key, char* value)
{
    if ((key == NULL) || (value == NULL)) {
        return ERROR_CODE_PARAM;
    }
    
    int length = IotMain_SendDataToJs();
    
    LOG_I("IotMain_JsGetClang:SetValue::entry--length=%d\r\n",length);

    if (length > 0 && length < CONFIG_MAX_SEND_DATA_LEN) {
        strncpy(value,g_sendMessage,length);
        value[length] = 0;
    } else {
        LOG_I(" IotMain_JsGetClang .value = NULL\r\n");
    }

    return NATIVE_SUCCESS;
}


static int IotMain_JsSetClang(const char* key, const char* value)
{
    if ((key == NULL) || (!IsValidValue(value))) {
        return ERROR_CODE_PARAM;
    }

    if(strncmp(key,"key_f1",strlen("key_f1")) == 0) {
        if(strncmp(value,"on",strlen("on")) == 0) {
            IotMain_JsSet(true);
        }else {
            IotMain_JsSet(false);
        }
    } else if(strncmp(key,"humidityValue",strlen("humidityValue")) == 0) {
        IotMain_JsSetHumidityThreshold(value);
    }

    return  NATIVE_SUCCESS;
}

void IotMain_CloudSet(bool value) {
    IotMain_SetSwitchSts(value);
    IotMain_CtrlSwitch(value);
}

int IotMain_GetHuidityThreshold(void)
{
    int threshold = g_appController.HumidityMax;
    if(threshold >= 40 && threshold <= 80)
    {
        return threshold;
    }
}

void IotMain_SetHuidityThreshold(int value)
{
    if(value >= 40 && value <= 80)
    {
        g_appController.HumidityMax = value;
    }
}
int  IotMain_GetCurDay(void)
{
    return g_appController.curDay;
}

int  IotMain_SendDataToJs(void)
{
    bool keySts = IotMain_GetMotorSts();
    int humidityValue = (int)i2c_GetHumidity();
    bool wifiSts = CLOUD_GetCloudConnectedStatus();


    char keyStr[8];
    char wifiStsStr[16];

    int l_lastTotalScheduleNumbers = g_appController.lastTotalScheduleNumbers ;
    int l_currTotalScheduleNumbers = g_appController.totalScheduleNumbers ;

    if (keySts) {
        strncpy(keyStr,"on",sizeof(keyStr));
    }else{
        strncpy(keyStr,"off",sizeof(keyStr));
    }

    if (wifiSts) {
        strncpy(wifiStsStr,"connected",sizeof(wifiStsStr));
    } else {
        strncpy(wifiStsStr,"disconnected",sizeof(wifiStsStr));
    }

    g_appController.humidityValue = humidityValue;
    g_appController.wifiSts = wifiSts;

    memset_s(g_sendMessage,CONFIG_MAX_SEND_DATA_LEN,0,CONFIG_MAX_SEND_DATA_LEN);

    if (l_lastTotalScheduleNumbers != l_currTotalScheduleNumbers) {
        int tm_hour =g_appController.scheduleDisplay[l_lastTotalScheduleNumbers].startHour;
        int tm_min = g_appController.scheduleDisplay[l_lastTotalScheduleNumbers].startMinute;
        int durationMinutes = g_appController.scheduleDisplay[l_lastTotalScheduleNumbers].DurationMinutes;
        int humidifierStatus = g_appController.scheduleDisplay[l_lastTotalScheduleNumbers].status;

        if(tm_hour < 24 && tm_min < 60 && durationMinutes < CN_MINUTES_IN_DAY){
            int endtime = (tm_hour * 60 + tm_min + durationMinutes) % CN_MINUTES_IN_DAY;
            int end_hour = endtime / 60;
            int end_min = endtime % 60;

            snprintf(g_sendMessage,sizeof(g_sendMessage),\
                    "{\"time\":\"%02d:%02d\",\"endTime\":\"%02d:%02d\",\"HumidifierStatus\":\"%d\"}",\
                    tm_hour,tm_min,end_hour,end_min,humidifierStatus);
        }
        g_appController.lastTotalScheduleNumbers = g_appController.totalScheduleNumbers;
    } else {
        
        if (g_appController.getNtpTime == true  && (IotMain_GetWeatherStatus() == true)) {

            g_appController.getNtpTime = false;

            struct weather_info weatherInfo;

            weatherInfo = IotWeather_GetWeather();

            snprintf(g_sendMessage,sizeof(g_sendMessage),\
                    "{\"time\":\"%02d:%02d\",\"date\":\"%d-%02d-%02d\",\"week\":\"%d\",\"weatherIndex\":\"%d\",\"temperature\":\"%d\",\"humidityMax\":\"%3d\"}",\
                    g_appController.curSecondsInDay/3600, (g_appController.curSecondsInDay%3600)/60,\
                    g_appController.date.year, g_appController.date.month, g_appController.date.day,\
                    g_appController.date.week,weatherInfo.weatherIndex,weatherInfo.temperature,\
                    g_appController.HumidityMax);
        } else {
            snprintf(g_sendMessage,sizeof(g_sendMessage),"{\"key\":\"%s\",\"humidityValue\":\"%d\",\"wifi\":\"%s\",\"time\":\"%02d:%02d\"}",\
                     keyStr,humidityValue,wifiStsStr,g_appController.curSecondsInDay/3600, (g_appController.curSecondsInDay%3600)/60);
        }
    }

    RaiseLog(LOG_LEVEL_INFO, "g_sendMessage:%s.\n",g_sendMessage);
   
    int length = strlen(g_sendMessage);
    g_sendMessage[length] = 0;

    return length;
}

/**
 * @brief Convert miniseconds to system ticks
 * @param ms Indicates the mimiseconds to convert
 * @return Returns the corresponding ticks of specified time
 */
static uint32_t IotMain_Time2Tick(uint32_t ms)
{
    uint64_t ret;
    ret = ((uint64_t)ms * osKernelGetTickFreq()) / CN_MINISECONDS_IN_SECOND;
    return (uint32_t)ret;
}

static void IotMain_RtcTimeUpdate(void)
{
    //extern int SntpGetRtcTime(int localTimeZone, struct tm *rtcTime);
    struct tm rtcTime;
    int ret = SntpGetRtcTime(CONFIG_LOCAL_TIMEZONE,&rtcTime);

    RaiseLog(LOG_LEVEL_INFO,"Year:%d Month:%d Mday:%d Wday:%d Hour:%d Min:%d Sec:%d", \
            rtcTime.tm_year + 1900, rtcTime.tm_mon + 1, rtcTime.tm_mday,\
            rtcTime.tm_wday, rtcTime.tm_hour, rtcTime.tm_min, rtcTime.tm_sec);

    
    g_appController.date.year = rtcTime.tm_year + 1900;
    g_appController.date.month = rtcTime.tm_mon + 1;
    g_appController.date.day = rtcTime.tm_mday;
    g_appController.date.week = rtcTime.tm_wday;

    if (rtcTime.tm_wday > 0) {
        g_appController.curDay = rtcTime.tm_wday - 1;
    } else {
        g_appController.curDay = 6;
    }

    if (ret == 0) {
        g_appController.getNtpTime = 1;
    }

    g_appController.curSecondsInDay = rtcTime.tm_hour * CN_SECONDS_IN_HOUR + \
            rtcTime.tm_min * CN_SECONDS_IN_MINUTE + rtcTime.tm_sec + 8; /* add 8 s offset */
}

/**
 * @brief Sensor data collection task entry and the main flow is as follows:
 *        1, Wait the for the event to trigger in specified time
 *        2, Collect Sensor Data (air humidity/temperature, soil moisture, etc.)
 *        3, Report the collected data to the Cloud Platform
 */
static void IotMain_DataCollectAndReportThread(const void *arg)
{
    (void)arg;
    uint32_t waitTicks;

    waitTicks = IotMain_Time2Tick(CONFIG_SENSOR_SAMPLE_CYCLE);
    
    RaiseLog(LOG_LEVEL_INFO, "----------------------waitTicks:%d --------------------",waitTicks);

    while (1) {
        uint32_t switchEvent = osEventFlagsWait(g_appController.switchEvent, CN_LAMP_EVENT_ALL, \
            osFlagsWaitAny, waitTicks);
        if (switchEvent & CN_LAMP_EVENT_ALL) {
            RaiseLog(LOG_LEVEL_INFO, "GetEvent:%08x", switchEvent);
            if(switchEvent & CN_SWITCH_EVENT_SETSTATUS) {
                IotMain_CtrlSwitch(g_appController.switchSts);
                (void) IotProfile_Report( g_appController.switchSts,g_appController.humidityValue, g_appController.HumidityMax);

                RaiseLog(LOG_LEVEL_INFO, "Get the sensor data:switchSts:%d humidity:%d HumidityHig:%d",\
                    g_appController.switchSts, g_appController.humidityValue, g_appController.HumidityMax);
            }
        }
    }
    return;
}

void IotMain_TimeCalcTimerThread(void *args)
{
    (void)args;
    g_appController.curSecondsInDay++;
    if (g_appController.timerFlag) {
         if (g_appController.executeTime > 0) {
             g_appController.executeTime--;
         }
    }
}


static void IotMain_SmartHumidifierSheduleThread(void)
{
    int startSecondInDay = 0;
    int endSecondInDay = 0;

    CommandParamSetSwitch switchStatus, lastSwitchStatus;
    lastSwitchStatus.status = 0;
    switchStatus.status = 0;

    while (1) {
        osDelay(IotMain_Time2Tick(CN_MINISECONDS_IN_SECOND));
        RaiseLog(LOG_LEVEL_INFO, "CurrentTime %02d:%02d:%02d\n", g_appController.curSecondsInDay/3600,\
            (g_appController.curSecondsInDay%3600)/60, g_appController.curSecondsInDay%60);
        if (g_appController.curSecondsInDay >= CN_SECONS_IN_DAY) {
            g_appController.curSecondsInDay = 0;
            g_appController.curDay++;
            if (g_appController.curDay >= EN_DAYALL) {
                g_appController.curDay = EN_MONDAY;
            }
            IOT_ScheduleSetUpdate(1);
        }
        printf("[%s|%d] \n", __func__, __LINE__);
        
        printf("g_appController.curDay = %d \n", g_appController.curDay);
        if (IOT_ScheduleIsUpdate(g_appController.curDay, g_appController.curSecondsInDay)) {
            RaiseLog(LOG_LEVEL_INFO,"[%s|%d] \n", __func__, __LINE__);
            if (g_appController.timerFlag > 0) {
                g_appController.timerFlag = 0;
                g_appController.executeTime = 0;
                if (g_appController.switchSts != lastSwitchStatus.status) {
                    g_appController.switchSts = lastSwitchStatus.status;
                    osEventFlagsSet(g_appController.switchEvent, CN_SWITCH_EVENT_SETSTATUS);
                }
            }
            startSecondInDay = IOT_ScheduleGetStartTime();
            endSecondInDay = startSecondInDay + IOT_ScheduleGetDurationTime();
            IOT_ScheduleGetCommand(&(switchStatus.status), NULL);
        }

        printf("[%s|%d] \n", __func__, __LINE__);
        if (g_appController.timerFlag == 0) {
            RaiseLog(LOG_LEVEL_INFO,"start:%d end:%d cur:%d",startSecondInDay, endSecondInDay,\
                g_appController.curSecondsInDay);
            if ((g_appController.curSecondsInDay  >= startSecondInDay) && \
                (g_appController.curSecondsInDay < endSecondInDay)) {    // the schedule time has coming
                lastSwitchStatus.status = g_appController.switchSts;
                g_appController.switchSts = switchStatus.status;

                if (g_appController.switchSts != lastSwitchStatus.status) {
                    RaiseLog(LOG_LEVEL_INFO, "Triggering");
                    osEventFlagsSet(g_appController.switchEvent, CN_SWITCH_EVENT_SETSTATUS);
                }
                g_appController.executeTime = endSecondInDay - g_appController.curSecondsInDay;
                g_appController.timerFlag = 1;
            }
        } else {
            RaiseLog(LOG_LEVEL_INFO,"Shedule works finished:g_appController.start:%d end:%d cur:%d excutetimeLeft:%d",\
                startSecondInDay, endSecondInDay, g_appController.curSecondsInDay, g_appController.executeTime);
            if (g_appController.executeTime == 0) {

                RaiseLog(LOG_LEVEL_INFO,"Shedule works finished:switchSts:%d lastSwitchStatus.status:%d.",\
                        g_appController.switchSts, lastSwitchStatus.status);

                if (g_appController.switchSts != lastSwitchStatus.status) {
                    g_appController.switchSts = lastSwitchStatus.status;
                    osEventFlagsSet(g_appController.switchEvent, CN_SWITCH_EVENT_SETSTATUS);
                }
                IOT_ScheduleSetUpdate(1);
                startSecondInDay = 0;
                endSecondInDay = 0;
                g_appController.timerFlag = 0;
            }
        }
    }

    return;
}

static int IotWifiInfo_get(char *ssid, int id_size, char *pwd, int pd_size)
{
    if (UtilsSetEnv("/data/") != 0) {
      LOG_E("%s %d:store ssid failed! \n", __FUNCTION__, __LINE__);
      return;
    }
    int retval = UtilsGetValue(SID_KEY, ssid, id_size);
    if (retval <= 0) {
        LOG_E("%s %d:no such ssid stored! \n", __FUNCTION__, __LINE__);
        return 0;
    }

    if (UtilsGetValue(PWD_KEY, pwd, pd_size) < 0) {
        LOG_E("%s %d:ssid(%s) no password stored! \n", __FUNCTION__, __LINE__, ssid);
    } else {
        LOG_E("%s %d:ssid : %s, pwd : %s! \n", __FUNCTION__, __LINE__, ssid, pwd);
    }

    return 1;
}

static void IotWifiInfo_set(char *ssid, char *pwd)
{
    if (UtilsSetEnv("/data/") != 0) {
        LOG_E("%s %d:store ssid failed! \n", __FUNCTION__, __LINE__);
        return;
    }
    if (UtilsSetValue(SID_KEY, ssid) != 0) {
        LOG_E("%s %d:store ssid failed! \n", __FUNCTION__, __LINE__);
        return;
    }
    if (UtilsSetValue(PWD_KEY, pwd) != 0) {
       LOG_E("%s %d:store password failed! \n", __FUNCTION__, __LINE__);
       UtilsDeleteValue(SID_KEY);
        return;
    }
    LOG_I("%s %d:store password success! \n", __FUNCTION__, __LINE__);
}

static void IotMain_MainTask(void *arg)
{
    int ret = 0;
    NfcInfo nfcInfo;
    char ssid[BUFF_SIZE] = {0};
    char pwd[BUFF_SIZE] = {0};

    IOT_ScheduleInit();
    IotMain_Init();
    i2c_Init();
    IotUart_StartUartThread(IotMain_VoiceSet);

    g_appController.switchEvent = osEventFlagsNew(NULL);
    if (g_appController.switchEvent == NULL) {
        RaiseLog(LOG_LEVEL_INFO, "---------------------g_appController.switchEvent == NULL--------------------");
        return;
    }
    IotMain_StartThreadOfDataCollectAndReport();

    nfcInfo.deviceID = "61b953b52b2aa20288c07fa9_SmartHumidifier01";
    nfcInfo.devicePWD = "12345678";

	ret =  IotWifiInfo_get(ssid, sizeof(ssid), pwd, sizeof(pwd));
	if (ret == 0) {
		if (BOARD_NetCfgStartConfig(SOFTAP_NAME, ssid, sizeof(ssid), pwd, sizeof(pwd)) < 0) {
			LOG_E("BOARD_NetCfgStartConfig failed! \n");
			return;
		} else {
			ret = AFTER_NETCFG_ACTION;
		}
	} 

    if (BOARD_ConnectWifi(ssid, pwd) != 0) { 
        LOG_E("BOARD_ConnectWifi failed! \n");
        return;
    }

    if (ret == AFTER_NETCFG_ACTION) {
        LOG_I("Connect wifi success ! \n");
        IotWifiInfo_set(ssid, pwd);
    }

    HILOG_INFO(HILOG_MODULE_APP, "IotMainTaskEntry BOARD_ConnectWifi %s\r\n", __func__);

    IotMain_RtcTimeUpdate();
    
    if (CLOUD_Init() != 0) {
        LOG_E("CLOUD_Init failed, return.");
        return;
    }

    if (CLOUD_Connect(nfcInfo.deviceID, nfcInfo.devicePWD, \
        CONFIG_CLOUD_DEFAULT_SERVERIP, CONFIG_CLOUD_DEFAULT_SERVERPORT) != 0) {
        RaiseLog(LOG_LEVEL_INFO, "---------------------CLOUD_Connect error!--------------------");
        return;
    }

    g_appController.timeID = osTimerNew(&IotMain_TimeCalcTimerThread, osTimerPeriodic, NULL, NULL);
    osTimerStart(g_appController.timeID, TIMER_TICK_NUM);

    LOG_I(LOG_LEVEL_INFO, "IotMainTaskEntry while\r\n");
    IotWeather_TaskCreate();

    IotMain_StartThreadOfHumidifierShedule();
}

void IotMain_StartMainThread(void)
{
    HILOG_INFO(HILOG_MODULE_APP, "HILOG_INFO %s\r\n", __func__);
    JsGetClangFuncationRegister(IotMain_JsGetClang);
    JsSetClangFuncationRegister(IotMain_JsSetClang);
    osThreadAttr_t attr = {0};
    attr.stack_size = 1024*10;
    attr.priority = 25;
    attr.name = "IOTMainThread";
    if (osThreadNew((osThreadFunc_t)IotMain_MainTask, NULL, &attr) == NULL) {
        HILOG_ERROR(HILOG_MODULE_APP, "Failed to create driver_test_task\r\n");
    }
}

void IotMain_StartThreadOfHumidifierShedule(void)
{
    HILOG_INFO(HILOG_MODULE_APP, "HILOG_INFO entry %s\r\n", __func__);
    osThreadAttr_t attr = {0};
    attr.stack_size = 1024*4;
    attr.priority = CONFIG_TASK_LEVEL;
    attr.name = "SmartHumidifierShedule";
    if (osThreadNew((osThreadFunc_t)IotMain_SmartHumidifierSheduleThread, NULL, &attr) == NULL) {
        HILOG_ERROR(HILOG_MODULE_APP, "Failed to create driver_test_task\r\n");
    }
}

void IotMain_StartThreadOfDataCollectAndReport(void)
{
    HILOG_INFO(HILOG_MODULE_APP, "HILOG_INFO %s\r\n", __func__);
    osThreadAttr_t attr = {0};
    attr.stack_size = 1024*4;
    attr.priority = CONFIG_TASK_LEVEL;
    attr.name = "SmartHumidifierReportData";

    if (osThreadNew((osThreadFunc_t)IotMain_DataCollectAndReportThread, NULL, &attr) == NULL) {
        HILOG_ERROR(HILOG_MODULE_APP, "Failed to create driver_test_task\r\n");
    }
}

